//#extension GL_OES_standard_derivatives : enable
#ifdef GL_OES_standard_derivatives
# extension GL_OES_standard_derivatives: enable
# endif
precision highp float; //指定浮点数精度为低精度

vec2 dHdxy_fwd(sampler2D normMap, vec2 uv, float intensity) {
	vec2 dSTdx = dFdx(uv);
	vec2 dSTdy = dFdy(uv);
	float bumpScale = intensity; //纹理强度
	float Hll = bumpScale * texture2D(normMap, uv).x;
	float dBx = bumpScale * texture2D(normMap, uv + dSTdx).x - Hll;
	float dBy = bumpScale * texture2D(normMap, uv + dSTdy).x - Hll;
	return vec2(dBx, dBy);
}
vec3 perturbNormalArb(vec3 surf_pos, vec3 surf_norm, vec2 dHdxy) {
	vec3 vSigmaX = vec3(dFdx(surf_pos.x), dFdx(surf_pos.y), dFdx(surf_pos.z));
	vec3 vSigmaY = vec3(dFdy(surf_pos.x), dFdy(surf_pos.y), dFdy(surf_pos.z));
	vSigmaY *= 12.0; //这个值不知道做什么的，但是设太小手机端显示范围会被切掉，应该是显示距离用的
	vec3 vN = surf_norm;
	vec3 R1 = cross(vSigmaY, vN);
	vec3 R2 = cross(vN, vSigmaX);
	float fDet = dot(vSigmaX, R1);
	fDet *= (float(gl_FrontFacing) * 2.0 - 1.0);
	vec3 vGrad = sign(fDet) * (dHdxy.x * R1 + dHdxy.y * R2);
	return normalize(abs(fDet) * surf_norm - vGrad);
}

mat3 MatTtoW(vec3 normal, vec4 tangent) {
	//切线转世界矩阵
	vec3 N = normal;
	vec3 T = normalize(tangent.xyz - N * tangent.xyz * N);
	vec3 B = cross(N, T);
	mat3 matTtoW = mat3(T, B, N);
	return matTtoW;
}

//varying vec3 outWorldPosition;
//varying vec4 outColor;
varying vec2 outTexCoord0;
varying vec2 outTexCoord1;
varying vec3 outWorldNormal;
//varying vec4 outTangent;
//varying mat3 outMatTtoW;

uniform vec3 lightDir;
uniform vec3 viewDir;
uniform vec3 lightColor;
uniform vec3 ambientColor;


/**
 * 兰伯特光照
 * @param {Object} vec3 normal
 * @param {Object} vec3 lightDir
 * @param {Object} float smoothness 平滑度
 * @param {Object} float decay 衰减度
 * @param {Object} float intensity 强度
 */
float Lambert(vec3 normal, vec3 lightDir, vec3 value) {
	float smoothness = value.x;
	float decay = value.y;
	float intensity = value.z;
	float lambert = dot(normal, lightDir) * intensity;
	lambert = max(0.0, lambert);
	lambert = pow(lambert, smoothness);
	lambert -= decay;
	lambert *= intensity;
	
//	lambert = max(0.0, min(1.0, lambert));
	
	return lambert;
}


/**
 * 高光
 * @param {Object} vec3 normal
 * @param {Object} vec3 lightDir
 * @param {Object} vec3 viewDir
 * @param {Object} float smoothness 平滑度
 * @param {Object} float decay 衰减度
 * @param {Object} float intensity 强度
 */
float Specular(vec3 normal, vec3 lightDir, vec3 viewDir, vec3 value) {
	float smoothness = value.x;
	float decay = value.y;
	float intensity = value.z;
	vec3 specular_h = normalize(lightDir + viewDir);
	float specular_nh = max(0.0, dot(normal, specular_h));
	float specular = pow(specular_nh, smoothness);
	specular -= decay;
	specular *= intensity;
//	specular = max(0.0, min(1.0, specular));
	specular = max(0.0, specular);
	return specular;
}

/**
 * 菲涅耳
 * @param {Object} vec3 normal
 * @param {Object} vec3 viewDir
 * @param {Object} float smoothness
 * @param {Object} float decay
 * @param {Object} float intensity
 */
float Fresnel(vec3 normal, vec3 viewDir, vec3 value) {
	float smoothness = value.x;
	float decay = value.y;
	float intensity = value.z;
	float fresnel_dn = 1.0 - dot(normal, viewDir);
	float fresnel = pow(fresnel_dn, smoothness);
	fresnel -= decay;
	fresnel *= intensity;
//	fresnel = max(0.0, min(1.0, fresnel));
	fresnel = max(0.0, fresnel);
	return fresnel;
}

//控制RGB的范围在0到1
vec3 standardRGB(vec3 rgb){
	float rgbR = max(0.0, min(1.0, rgb.r));
	float rgbG = max(0.0, min(1.0, rgb.g));
	float rgbB = max(0.0, min(1.0, rgb.b));
	vec3 outRgb = vec3(rgbR,rgbG,rgbB);
	return outRgb;
}


//调制RGB效果
//value.x = smoothness ,value.y = decay ,value.z = intensity 
vec3 ModulationRGB(vec3 rgb,vec3 value){
	float rgbR = rgb.r;
	float rgbG = rgb.g;
	float rgbB = rgb.b;
	float smoothness = value.x;
	float decay = value.y;
	float intensity = value.z;
	rgbR = pow(rgbR, smoothness);
	rgbG = pow(rgbG, smoothness);
	rgbB = pow(rgbB, smoothness);
	rgbR -= decay;
	rgbG -= decay;
	rgbB -= decay;
	rgbR *= intensity;
	rgbG *= intensity;
	rgbB *= intensity;
	vec3 outRgb = vec3(rgbR,rgbG,rgbB);
	return outRgb;
}

//调色	
vec3 ColorRGBA(vec3 a, vec4 rgba){
	return a * rgba.rgb * rgba.a;
}

vec2 MatcapCoord(vec3 worldNormal){
	vec2 matcapCoord = (worldNormal.xy*0.5+0.5);
	matcapCoord = vec2(matcapCoord.x,-matcapCoord.y);
	return matcapCoord;
}

//旧版本用的，到时要换掉
vec3 MapNormal(sampler2D textureNormal,vec2 texCoord){
	vec3 mapNormal2 = texture2D(textureNormal,texCoord).rgb;
	mapNormal2 = (mapNormal2.xyz*2.0-1.0);
//	mapNormal2 = outMatTtoW*mapNormal2;
	mapNormal2 = outWorldNormal + mapNormal2;
//	mapNormal2*=5.0;
	mapNormal2 = normalize(mapNormal2);
	return mapNormal2;
}

vec3 MapNormalToWorld(vec3 mapNormal2){
		mapNormal2 = (mapNormal2.xyz*2.0-1.0);
		mapNormal2 = outWorldNormal + mapNormal2;
		mapNormal2 = normalize(mapNormal2);
		return mapNormal2;
}

vec3 MapNormalIntensity(vec3 mapNormal, float intensity){
	vec3 normal = outWorldNormal*(1.0-intensity) + mapNormal*intensity;
	return normal;
}


vec3 LerpVec3(vec3 a,vec3 b,float value){
	vec3 rgb = a*(1.0-value) + b*value;
	return rgb;
}


vec2 TRANSFORM_TEX(vec2 uv,vec4 tilingOffset){
	uv *= tilingOffset.xy;
	uv += tilingOffset.zw;
	return uv;
}


